package com.tongzhou.util;

import java.util.Map;

/**
 * 
 * 对会话的操作
 * 
 */

public class MemSession
{
	// 会话ID
	private String sid = "";

	// 存放本会话的所有信息
	private Map map = null;

	public static MemSession getSession(String sid)
	{
		MemSession session = null;

		session = new MemSession(sid, true);

		return session;
	}
	
	public static MemSession getNoExpirySession(String sid)
	{
		MemSession session = null;

		session = new MemSession(sid);

		return session;
	}
	
	
	public static MemSession getSession(String sid,int expiryTime)
	{
		MemSession session = null;

		session = new MemSession(sid, true, expiryTime);

		return session;
	}
	
	public static MemSession getSession(String sid, boolean create)
	{
		MemSession session = null;

		session = new MemSession(sid, create);

		return session;
	}

	private MemSession(String sid)
	{
		this.sid = sid;
		this.map = SessionService.getInstance().getSession(sid);
	}
	
	
	private MemSession(String sid, boolean create)
	{
		this.sid = sid;
		this.map = SessionService.getInstance().getSession(sid, create);
	}
	
	private MemSession(String sid, boolean create, int expiryTime)
	{
		this.sid = sid;
		this.map = SessionService.getInstance().getSession(sid, create, expiryTime);
	}
	

	public static boolean sessionExists(String sid)
	{
		return SessionService.getInstance().sessionExists(sid);
	}

	public Object getAttribute(String arg0)
	{
		if( this.map != null )
		   return this.map.get(arg0);
		else
		   return null;
	}

	public void invalidate()
	{
		if( this.map != null )
		    this.map.clear();
		SessionService.getInstance().removeSession(this.sid);
	}

	public void removeAttribute(String arg0)
	{
		if (arg0 == null || arg0.trim().length() <= 0)
		{
			return;
		}
		if( this.map != null )
		    this.map.remove(arg0);
		SessionService.getInstance().saveSession(this.sid, this.map);
	}

	public void setAttribute(String arg0, Object arg1)
	{
		if (arg0 == null || arg0.trim().length() <= 0 || arg1 == null)
		{
			return;
		}
		this.map.put(arg0, arg1);
		SessionService.getInstance().saveSession(this.sid, this.map);
	}

	public void setAttribute(String key, Object obj, int expiryTime)
	{
		if (key == null || key.trim().length() <= 0 || obj == null)
		{
			return;
		}
		
		this.map.put(key, obj);
		SessionService.getInstance().saveSession(this.sid, this.map, expiryTime);
	}
	
	
	public void setNoExpiryAttribute(String key, Object obj)
	{
		if (key == null || key.trim().length() <= 0 || obj == null)
		{
			return;
		}
		
		this.map.put(key, obj);
		SessionService.getInstance().saveNoExpirySession(this.sid, this.map);
	}
	
	public static void updateExpiryDate(String sid)
	{
		SessionService.getInstance().updateExpiryDate(sid);
	}

}
